<?php

namespace App\Http\Controllers\Business;

use App\Common\Traits\Model\ModelSearchTrait;
use App\Http\Controllers\Controller;
use App\Models\Business\SubAccountSource;
use Illuminate\Http\Request;

class SubAccountSourceController extends Controller
{
    use ModelSearchTrait;

    //
    public function index(Request $request, SubAccountSource $accountSource)
    {
        $accountSource=$this->singleModelSearch($request,$accountSource,$this->platformCondition());
        $platforms=$accountSource->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        return $this->success($platforms);
    }
    //
    public function platformCondition()
    {
        return [
            ['source_name','like']
        ];
    }

    //
    public function store(Request $request)
    {
        if(!$request->filled('source_name')){
            return $this->failed('来源名称不能为空',201);
        }
        $data=['source_name'=>mb_substr($request->source_name,0,64)];
        SubAccountSource::create($data);
        return $this->success(['msg'=>'添加成功']);
    }

    //
    public function show(Request $request)
    {
        $show=SubAccountSource::find($request->source_id??0);
        return $this->success($show);
    }

    //
    public function update(Request $request, SubAccountSource $accountSource)
    {
        if(!$request->filled('source_name')){
            return $this->failed('来源名称不能为空',201);
        }
        $platform=$accountSource->find($request->source_id??0);
        if(empty($platform)){
            return $this->failed('id错误，无此来源',201);
        }
        $platform->source_name=mb_substr($request->source_name,0,64);
        $platform->save();
        return $this->success(['msg'=>'编辑成功']);
    }

    //
    public function destroy(Request $request, SubAccountSource $accountSource)
    {
        $platform=$accountSource->find($request->source_id??0);
        if(empty($platform)){
            return $this->failed('id错误，无此来源',201);
        }
        $platform->delete();
        return $this->success(['msg'=>'删除成功']);
    }

    //
    public function getAllSources(){
        $sources=SubAccountSource::all();
        return $this->success($sources);
    }


}
